package com.gdwz.ps.controller;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import xz.facade.user.model.Result;

import com.xzsoft.gdwz.model.TrdPsEmInfLog;
import com.xzsoft.gdwz.services.TrdPsEmInfLogService;
import com.xzsoft.gdwz.services.sendLimitVendorService;

@Controller
@RequestMapping("/vendor")
public class LimitVendorController {
	
	@Autowired
	private sendLimitVendorService sendLimitVendorService;
	
	@Autowired
	private TrdPsEmInfLogService trdPsEmInfLogService;
	
	
	 /**
	  * 接受限时报价供应商
	  * @param request
	  * @param response
	 * @throws IOException 
	  */
	 @RequestMapping(value="/reciveVendor",produces="text/html;charset=UTF-8")
	 public void reciveLimitVendor(HttpServletRequest request,HttpServletResponse response) throws IOException{

		 String vendorInfo = request.getParameter("vendor_info");
		 Result result = new Result();
		 String ename = null;
		 //解析jasonarray 
		 JSONArray resultArray = null;
		try {
			resultArray = JSONArray.fromObject(vendorInfo);
		} catch (Exception e) {
			e.printStackTrace();
			result.setResult("");
			result.setResultCode("C0011");
			result.setResultMessage("JSONArray格式不正確！");
			result.setSuccess(false);
			JSONObject jb = JSONObject.fromObject(result);
			response.getWriter().println(jb.toString());
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		 try {
			//保存限时报价供应商		
			// sendLimitVendorService.GHsendLimitVendor(resultArray);
		} catch (Exception e) {
			e.printStackTrace();
			result.setResult("");
			result.setResultCode("C0012");
			result.setResultMessage(e.getMessage());
			result.setSuccess(false);
			JSONObject jb = JSONObject.fromObject(result);
			response.getWriter().println(jb.toString());
			response.getWriter().flush();
			response.getWriter().close();
			//保存日志
			TrdPsEmInfLog log = new TrdPsEmInfLog();
			log.setId(UUID.randomUUID().toString());
			log.setEmName(ename);
			log.setErrorMsg(e.getMessage());
			log.setHasRecieve("0");
			if(e.getMessage().contains("|")){
				String orderId = e.getMessage().split("|")[1];
				log.setOrderId(orderId);
				log.setErrorMsg(e.getMessage().split("|")[0]);
			}
			log.setOrderInfo(resultArray.toString());
			trdPsEmInfLogService.insertTrdPsEmInfLog(log);
			return;
		}
			result.setResult("");
			result.setResultCode("");
			result.setResultMessage("");
			result.setSuccess(true);
			JSONObject jb = JSONObject.fromObject(result);
			response.getWriter().println(jb.toString());
			response.getWriter().flush();
			response.getWriter().close();
	 }
	 
}
